#!/usr/bin/env python
import requests
import pytest
import allure
import json
import jsonpath
from hashlib import md5
from faker import Faker
from base.get_request_response import get_request_url, record_allure_log, allure_step_url, allure_step_request_tool_get, \
    allure_step_response_json, allure_step_extract_data, allure_step_assert_response, allure_step_url_data, \
    allure_step_request_tool_post
from util.report import log_tool
from base import globalvars as glo
from base.getdata import get_header

def authorize():
    headers = get_header("", label_name="header1")
    url = allure_step_url('/api/user/oauth/authorize', headers)
    req = {"emailOrUsername": "17723811700",
            "password": "lQZ94osH3OXT2U8PTqonG2IcapsSot94brB2Ou5pM26v1X+wto6iLXfNW8zu1Gq9QLsyP1njh2JyKvHo6YWZj/mnaR8IBjHxtplKez4zq6h9IcdwEdr8yDxlppBRq63Vvh6HFuc2gbPJTdASb54CF/otYa2VroA5j63xvHJ/0pW9TR+Krl+/mZ7UpSshWvybIRmbnzqoipUhlOIRP1/im9QG/fsUNJbf5WV4sHYnP4267cOuyZ7cJx6DiAzoAY+XYBlmxIj5humQIPGEl/ez4/wBcUTS0TiXZxZeNzkRfutP+ISddOcyTT1Hqw+CUJsRgasJnwNwnNdhWAGq7okkNQ==",
            "type": 2,
           "clientId": "b4cb201e6f30269704e022325ea52e59",
           }
    resp = allure_step_request_tool_post(url, headers, req)
    data = allure_step_response_json(resp)

    with allure.step('第4步：判断结果'):
        allure.attach("resp.status_code == 200", '断言条件', allure.attachment_type.TEXT)
        log_tool.info('\n断言条件：\n' + 'resp.status_code == 200')
        assert resp.status_code == 200
        assert jsonpath.jsonpath(data, '$..userToken')[0] is not None

    with allure.step('第5步：提取数据'):
        # pub_data['data_name']='data_value';
        allure.attach('无', '提取数据列表', allure.attachment_type.TEXT)
        log_tool.info('\n提取数据列表：\n' + '无')
        log_tool.info('\n\n\n')
        token = 'qab4cb201e6f30269704e022325ea52e59' + '=' + data["userToken"]
        return token

@pytest.fixture(scope='session')
def pub_data():
    data={"login": authorize()}
    yield data
